package com.guo.hui.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageInfo;
import com.guo.hui.dao.blog.TpArticleMapper;
import com.guo.hui.model.blog.TpArticle;
import com.guo.hui.page.PageParam;
import com.guo.hui.page.PageResult;
import com.guo.hui.service.BlogService;

@Controller
@RequestMapping("/blogweb")
public class BlogWebController {

	@Autowired
	private BlogService blogService;
	@Autowired
	private TpArticleMapper articleDao;

	@RequestMapping("/index")
	public String index(Model mm, String type, @RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
		PageParam pageParm = new PageParam();
		pageParm.setPage(page);
		pageParm.setPageSize(pageSize);
		Map<String, Object> map = new HashMap<>();
		map.put("cateid", type);
		pageParm.setSearchContent(map);
		PageInfo<TpArticle> pagelist = blogService.selectByMapPage(pageParm);
		PageResult result = new PageResult();
		result.setTotal((int) pagelist.getTotal());
		result.setRows(pagelist.getList());
		result.setType(type);
		result.setPageTotal(1);
		if (result.getTotal() > 0) {
			result.setPageTotal(result.getTotal() / pageSize + 1);
		}
		mm.addAttribute("result", result);
		mm.addAttribute("page", pagelist);
		return "blog/web/index";
	}

	@RequestMapping("/artdetail")
	public String artdetail(Model mm, @RequestParam Integer id) {
		TpArticle tpArticle = articleDao.selectById(id);
		mm.addAttribute("tp", tpArticle);
		return "blog/web/artdetail";
	}

	// @RequestMapping("/index")
	// public String login(Model mm) {
	// Map<String, Object> map = new HashMap<>();
	// List<TpArticle> list = blogService.selectByMap(map);
	// mm.addAttribute("list", list);
	// return "blog/index";
	// }

}
